package com.xoa.service.sys.impl;

import com.xoa.dao.sys.SealLogMapper;
import com.xoa.dao.sys.SealMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.sys.Seal;
import com.xoa.model.sys.SealLog;
import com.xoa.model.sys.SealWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.service.sys.SealLogService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建作者:   高亚峰
 * 创建日期:   2017/8/22 11:20
 * 类介绍  :   印章日志处理
 * 构造参数:
 */
@Service
public class SealLogServiceImpl implements SealLogService {

    @Resource
    private SealLogMapper sealLogMapper;

    @Resource
    private SealMapper sealMapper;

    @Resource
    private UsersMapper usersMapper;

    @Override
    public int addSealLog(SealLog sealLog) {
       return sealLogMapper.insertSelective(sealLog);
    }

    @Override
    public ToJson<SealLog> getAllSealLog(String startTime,String endTime,HttpServletRequest request,String sealName,SealLog sealLog,Integer page,Integer pageSize,boolean useFlag) {
        ToJson<SealLog> json =new ToJson<SealLog>(1,"err");
        try {
            if(startTime==""){
                startTime=null;
            }
            if(endTime==""){
                endTime=null;
            }
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            StringBuffer sb =new StringBuffer();
            String id ="";
            if(sealName!=null && sealName!=""){
                List<SealWithBLOBs> sealByName = sealMapper.getSealByName(sealName);
                for(SealWithBLOBs seal:sealByName){
                  id=sb.append(seal.getSealId()+",").toString();
                }
                id=id.substring(0,id.length()-1);
            }
            sealLog.setsId(id);
            map.put("sealLog",sealLog);
            List<SealLog> allSealLogInfo = sealLogMapper.getAllSealLogInfo(map);
            if(allSealLogInfo!=null){
            for(SealLog sealLog1:allSealLogInfo){
                Seal sealLogBySealId = sealMapper.getSealLogBySealId(sealLog1.getsId());
                if(sealLogBySealId!=null){
                sealLog1.setSealName(sealLogBySealId.getSealName());
                }else{
                    sealLog1.setSealName("印章已删除");
                }
                Users usersByuserId = usersMapper.getUsersByuserId(sealLog1.getUserId());
                sealLog1.setUserName(usersByuserId.getUserName());
                //操作日志类型
                if(sealLog1.getLogType().equals("makeseal")){
                   sealLog1.setLogTypeName("制作印章");
                }
                else if(sealLog1.getLogType().equals("setseal")){
                    sealLog1.setLogTypeName("印章授权");
                }
                else if(sealLog1.getLogType().equals("deleteseal")){
                    sealLog1.setLogTypeName("删除印章");
                }
            }
            }
            json.setFlag(0);
            json.setMsg("ok");
            json.setObj(allSealLogInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<Object> deleteSealLog(String[] ids) {
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            sealLogMapper.deleteSealLog(ids);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
